/**
 *
 * Licensed Materials - Property of IBM 
 * 
 * OCO Source Materials 
 * 
 * (C) Copyright IBM Corp. 2011, 2012 All Rights Reserved 
 * 
 * The source code for this program is not published or other- 
 * wise divested of its trade secrets, irrespective of what has 
 * been deposited with the U.S. Copyright Office. 
 *
 */

/**
 * CHANGE HiSTORY:
 * Author             Time                 Description
 *
 */
package com.ibm.ba.demo.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ibm.ba.demo.entity.BaDemoScoreResult;
import com.ibm.ba.demo.entity.SPSSModelConfiguration;
import com.ibm.ba.demo.helper.BeanHelper;

@Repository
public class InDBScoreDao extends AbstractDao {
	private static Logger logger = Logger.getLogger(InDBScoreDao.class);
	@Autowired
	private SPSSModelConfiguration modelConfig;
	private static String SQL_BATCH_INPUT = "INSERT INTO BATCHINPUT VALUES (:ORDERKEY,"
			+ ":L_PARTKEY_0,:L_PARTKEY_1,:L_PARTKEY_2,:L_PARTKEY_3,:L_PARTKEY_4,:L_PARTKEY_5,"
			+ ":L_PARTKEY_6,:L_PARTKEY_7,:L_PARTKEY_8,:L_PARTKEY_9,:L_PARTKEY_10,:L_PARTKEY_11,:L_PARTKEY_12,"
			+ ":L_PARTKEY_13,:L_PARTKEY_14,:L_PARTKEY_15,:L_PARTKEY_16,:L_PARTKEY_17,:L_PARTKEY_18,"
			+ ":L_PARTKEY_19,:L_PARTKEY_20,:L_PARTKEY_21,:L_PARTKEY_22,:L_PARTKEY_23,:L_PARTKEY_24,:L_PARTKEY_25,"
			+ ":L_PARTKEY_26,:L_PARTKEY_27,:L_PARTKEY_28,:L_PARTKEY_29,:L_PARTKEY_30,:L_PARTKEY_31,:L_PARTKEY_32,"
			+ ":L_PARTKEY_33,:L_PARTKEY_34,:L_PARTKEY_35,:L_PARTKEY_36,:L_PARTKEY_37,:L_PARTKEY_38,"
			+ ":L_PARTKEY_39,:L_PARTKEY_40,:L_PARTKEY_41,:L_PARTKEY_42,:L_PARTKEY_43,:L_PARTKEY_44,"
			+ ":L_PARTKEY_45,:L_PARTKEY_46,:L_PARTKEY_47,:L_PARTKEY_48,:L_PARTKEY_49)";
	private static String SQL_GET_SCORE = "SELECT CUSTKEY, C50, C51, C52,C53,C54,C55,C56,C57,C58 "
			+ "FROM (SELECT CUSTKEY,UNPACK(HUMSPSS.SCORE_COMPONENT('P',:modeludf,PACK(CCSID 1208,T0.C0,T0.C1,T0.C2,"
			+ "T0.C3,T0.C4,T0.C5,T0.C6,T0.C7,T0.C8,T0.C9,T0.C10,T0.C11,T0.C12,T0.C13,T0.C14,T0.C15,T0.C16,"
			+ "T0.C17,T0.C18,T0.C19,T0.C20,T0.C21,T0.C22,T0.C23,T0.C24,T0.C25,T0.C26,T0.C27,T0.C28,T0.C29,"
			+ "T0.C30,T0.C31,T0.C32,T0.C33,T0.C34,T0.C35,T0.C36,T0.C37,T0.C38,T0.C39,T0.C40,T0.C41,T0.C42,"
			+ "T0.C43,T0.C44,T0.C45,T0.C46,T0.C47,T0.C48,T0.C49))).* AS (C50 VARCHAR(12) CCSID UNICODE, "
			+ "C51 DOUBLE, C52 BIGINT, C53 VARCHAR(12) CCSID UNICODE, C54 DOUBLE, C55 BIGINT, "
			+ "C56 VARCHAR(12) CCSID UNICODE, C57 DOUBLE, C58 BIGINT) FROM ("
			+ "SELECT * FROM BATCHINPUT) AS T0) T1";

	@Transactional
	public List<BaDemoScoreResult> getScore(List<Map<String, Object>> paramList) {
		logger.info("getScore - paramList is: " + BeanHelper.beanToJson(paramList));
		populateBatchInput(paramList);
		return getScore();
	}

	@Transactional
	public BaDemoScoreResult getScore(Map<String, Object> paramMap) {
		logger.info("getScore - paramMap is: " + BeanHelper.beanToJson(paramMap));
		List<Map<String, Object>> paramList = new ArrayList<Map<String, Object>>();
		paramList.add(paramMap);
		return getScore(paramList).get(0);
	}
	@SuppressWarnings("unchecked")
	private void populateBatchInput(List<Map<String, Object>> paramList) {
		getJdbcTemplate().batchUpdate(SQL_BATCH_INPUT, (HashMap[]) paramList.toArray(new HashMap[paramList.size()]));
	}

	private List<BaDemoScoreResult> getScore() {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("modeludf", modelConfig.getSpssModelUDF());
		return getJdbcTemplate().query(SQL_GET_SCORE, paramMap,
				new InDBScoreResultSetExtractor());
	}
}
